package work.parabola.keyboardswitch.pojo.query.storeswitch;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestParam;
import work.parabola.keyboardswitch.common.ResponseCode;
import work.parabola.keyboardswitch.common.ServerResponse;
import work.parabola.keyboardswitch.util.BigDecimalUtil;

import java.math.BigDecimal;

@Data
@ToString
@EqualsAndHashCode
public class NoteQuery {

    private Integer id;

    private String note;

    private String salePrice;

    private BigDecimal salePriceDecimal;

    public ServerResponse validate() {
        if (id == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),
                    ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        if (StringUtils.isBlank(this.note)) {
            this.note = null;
        }
        try {
            this.salePriceDecimal = new BigDecimal(this.salePrice).setScale(2, BigDecimal.ROUND_HALF_UP);
        } catch (Exception e){
            return ServerResponse.createByErrorMessage("售卖价格设置错误");
        }
        return ServerResponse.createBySuccess();
    }
}
